//
//  LXCarAddViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/30.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXCarAddViewController.h"
#import "LXTableViewCell.h"
#import "LXChooseBtnTableViewCell.h"
#import "LXCarPicTableViewCell.h"
#import "LXChooseBrandViewController.h"
#import "PinPaiModel.h"
#import "LXAllCarListViewController.h"
#import "LXUploadCarNumTableViewCell.h"
#import "LXChooseCarNumCell.h"
#import "LXRemarkTabelViewCell.h"
@interface LXCarAddViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UIGestureRecognizerDelegate>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSMutableDictionary *loanDic;
@property (nonatomic) NSDictionary *selDic;
@property (nonatomic) NSMutableArray *operPlatformData; // 运行平台数据源
@property (nonatomic) NSInteger operPlatform; // 运行平台 （1：滴滴平台，2：美团平台，3：不限平台）
@property (nonatomic) NSMutableArray *leaseCarTypeData; // 车辆类型数据源
@property (nonatomic) NSInteger leaseCarType; // 车辆类型 （1：快车优享，2：专车，3：商务）
@property (nonatomic) NSMutableArray *energyTypeData; // 能源类型数据源
@property (nonatomic) NSInteger energyType; // 能源类型（1：纯电力，2：混合动力，3：燃油动力）
@property (nonatomic) NSMutableArray *carServiceTypeData; // 运营性质数据源
@property (nonatomic) NSInteger carServiceType; // 运营性质 （1：营运，2：非营运）
@property (nonatomic) NSArray *ascriptionData; // 车辆归属数据源
@property (nonatomic) NSInteger ascription; // 车辆归属 （1：企业，2：个人）
@property (nonatomic) NSString *frontUrl; // 行驶证正面url
@property (nonatomic) NSString *backUrl; // 行驶证背面url
@property (nonatomic) NSString *bevelUrl; // 行驶证斜面url
@property (nonatomic) NSString *carBrand,*carMode; // 品牌车型
@property (nonatomic) NSString *registerTime; // 注册时间

// 车牌号
@property (nonatomic) UIView *carNumView;
@property (nonatomic) NSString *carNumStr;
@property (nonatomic) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *carNumData;
@property (nonatomic) NSString *plateNo;

@end

@implementation LXCarAddViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self layoutCustomNav:self.view title:@"新增车辆"];
    self.view.backgroundColor = White_Color;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[LXTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXTableViewCell class])];
    [self.tableView registerClass:[LXChooseBtnTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXChooseBtnTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXCarPicTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXCarPicTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXUploadCarNumTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXUploadCarNumTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXRemarkTabelViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];

    
    [self loadData];
    
    
    [self setBottomView];
    
    [self.view addSubview:self.carNumView];
    
    
}

- (void)loadData{
    
    self.loanDic  = [NSMutableDictionary dictionary];
    
   
    // 运行平台 默认滴滴平台
    self.operPlatform = 1;
    // 车辆类型 默认快车优享受
    self.leaseCarType = 1;
    // 能源类型 默认纯电动
    self.energyType = 1;
    // 运营性质 默认运营
    self.carServiceType = 1;
    // 车辆归属 默认企业
    self.ascription = 1;

    self.data = @[
        @{
            @"title":@"行驶证",
            @"key":@"drivingLicense",
            @"height":@(150)
        },
        
        @{
            @"title":@"车牌号",
            @"key":@"carNum",
            @"placeHolder" : @"请输入车牌号",
            @"canEdit":@(YES),
            @"showAccess":@(NO),
            @"maxLength":@(8),
            @"sel":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"品牌型号",
            @"key":@"carBrand",
            @"placeHolder" : @"请选择品牌型号",
            @"canEdit":@(NO),
            @"showAccess":@(YES),
            @"maxLength":@(1000),
            @"sel":@"clickCarBrand",
            @"height":@(kComHeight)
        },
//        @{
//            @"title":@"车架号",
//            @"key":@"vinNo",
//            @"placeHolder" : @"请输入车架号",
//            @"canEdit":@(YES),
//            @"showAccess":@(NO),
//            @"maxLength":@(25),
//            @"sel":@"clickIntoTime",
//            @"height":@(kComHeight)
//        },
        @{
            @"title":@"注册日期",
            @"key":@"registerTime",
            @"placeHolder" : @"请选择注册日期",
            @"canEdit":@(NO),
            @"showAccess":@(YES),
            @"maxLength":@(1000),
            @"sel":@"clickRegisterTime",
            @"height":@(kComHeight)
        },
//        @{
//            @"title":@"发动机号",
//            @"key":@"engineNo",
//            @"placeHolder" : @"请输入发动机号",
//            @"canEdit":@(YES),
//            @"showAccess":@(NO),
//            @"maxLength":@(25),
//            @"sel":@"",
//            @"height":@(kComHeight)
//        },
        @{
            @"title":@"行驶里程(Km)",
            @"key":@"kilometers",
            @"placeHolder" : @"请输入公里数",
            @"canEdit":@(YES),
            @"keyboardType":@(UIKeyboardTypeDecimalPad),
            @"showAccess":@(NO),
            @"maxLength":@(7),
            @"sel":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"年检",
            @"key":@"yearCheckUpTimeScope",
            @"placeHolder" : @"请选择年检日期",
            @"canEdit":@(NO),
            @"showAccess":@(YES),
            @"maxLength":@(1000),
            @"sel":@"clickYearCheckUpTimeScope",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"保险",
            @"key":@"insureTime",
            @"placeHolder" : @"请选择保险日期",
            @"canEdit":@(NO),
            @"keyboardType":@(UIKeyboardTypeDecimalPad),
            @"showAccess":@(YES),
            @"maxLength":@(7),
            @"sel":@"clickInsureTime",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"运行平台",
            @"key":@"operPlatform",
            @"height":@([LXChooseBtnTableViewCell height:self.operPlatformData])
        },
        @{
            @"title":@"车辆类型",
            @"key":@"leaseCarType",
            @"height":@([LXChooseBtnTableViewCell height:self.leaseCarTypeData])
        },
        @{
            @"title":@"能源类型",
            @"key":@"energyType",
            @"height":@([LXChooseBtnTableViewCell height:self.energyTypeData])
        },
        @{
            @"title":@"营运性质",
            @"key":@"carServiceType",
            @"height":@([LXChooseBtnTableViewCell height:self.carServiceTypeData])
        },
        @{
            @"title":@"所有人",
            @"key":@"ascription",
            @"height":@([LXChooseBtnTableViewCell height:self.ascriptionData])
        },
        @{
            @"title":@"备注",
            @"key":@"remark",
            @"height":@(150)
        },

       
 ].mutableCopy;
    
    
   
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.data[indexPath.row];
    return [dic[@"height"] doubleValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dic = self.data[indexPath.row];

    // 行驶证
    if ([dic[@"key"] isEqualToString:@"drivingLicense"]) {

        LXCarPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXCarPicTableViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
        
        [cell setFrontValue:^(NSDictionary * _Nonnull info) {
           // 车牌号
            NSString *carNum = info[@"words_result"][@"号牌号码"][@"words"];
            if (carNum.length > 1) {
                [self.loanDic setValue:[carNum substringFromIndex:1] forKey:@"carNum"];
                self.carNumStr = [carNum substringToIndex:1];
            }
            // 车架号
            [self.loanDic setValue:info[@"words_result"][@"车辆识别代号"][@"words"] forKey:@"vinNo"];
            // 发动机号
             [self.loanDic setValue:info[@"words_result"][@"发动机号码"][@"words"] forKey:@"engineNo"];
            // 品牌  新版需要手动选择
//             [self.loanDic setValue:info[@"words_result"][@"品牌型号"][@"words"] forKey:@"carBrand"];
            // 注册日期
            NSString *registerTime = info[@"words_result"][@"注册日期"][@"words"];
            self.registerTime = registerTime;
           NSInteger time =  [NSString timeSwitchTimestamp:registerTime andFormatter:@"yyyyMMdd"];
            registerTime = [NSString timestampSwitchTime:time/1000 andFormatter:k_DateFormat];
            
             [self.loanDic setValue:registerTime forKey:@"registerTime"];
            
            self.frontUrl = info[@"frontUrl"];
            
            [self.tableView reloadData];
        }];
 
        [cell setOtherValue:^(NSDictionary * _Nonnull info) {
            if (!StringIsEmpty(info[@"backUrl"])) {
                self.backUrl = info[@"backUrl"];
            }
            else if(!StringIsEmpty(info[@"bevelUrl"])){
                self.bevelUrl = info[@"bevelUrl"];
            }
            
            
        }];
        return cell;
    }
    
 
    if ([dic[@"key"] isEqualToString:@"operPlatform"]||
        [dic[@"key"] isEqualToString:@"leaseCarType"]||
        [dic[@"key"] isEqualToString:@"energyType"]||
        [dic[@"key"] isEqualToString:@"carServiceType"]||
        [dic[@"key"] isEqualToString:@"ascription"]) {
       
        
        NSArray *tempAry;
        if ([dic[@"key"] isEqualToString:@"operPlatform"]) {
            tempAry = self.operPlatformData;
        }
        else if ([dic[@"key"] isEqualToString:@"leaseCarType"]) {
            tempAry = self.leaseCarTypeData;
        }
        else if ([dic[@"key"] isEqualToString:@"energyType"]) {
            tempAry = self.energyTypeData;
        }
        else if ([dic[@"key"] isEqualToString:@"carServiceType"]) {
            tempAry = self.carServiceTypeData;
        }
        else {
            tempAry = self.ascriptionData;
        }
        
        
       LXChooseBtnTableViewCell *cell = [[LXChooseBtnTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([LXChooseBtnTableViewCell class]) WithData:tempAry];

       cell.selectionStyle = UITableViewCellSelectionStyleNone;
       [cell setClipsToBounds:YES];
        cell.canEdit = YES;
     
       cell.titleLab.text = dic[@"title"];
       [cell setClickBtnBlock:^(NSInteger Id) {
           if ([dic[@"key"] isEqualToString:@"operPlatform"]) {
               self.operPlatform = Id;
           }
           else if ([dic[@"key"] isEqualToString:@"leaseCarType"]) {
               self.leaseCarType = Id;
           }
           else if ([dic[@"key"] isEqualToString:@"energyType"]) {
               self.energyType = Id;
           }
           else if ([dic[@"key"] isEqualToString:@"carServiceType"]) {
               self.carServiceType = Id;
           }
           else {
               self.ascription = Id;
           }
           
       }];
       
       
       
       return cell;
    }
    // 车牌号
    else if([dic[@"key"] isEqualToString:@"carNum"]){
        LXUploadCarNumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXUploadCarNumTableViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
        cell.titleLb.text = @"车牌号";
        cell.carNumTextF.text = self.loanDic[dic[@"key"]] ;
        [cell.carNumBtn setTitle:self.carNumStr forState:UIControlStateNormal];
        cell.textfiledChangeBlock = ^(NSString * _Nonnull textStr) {
            self.loanDic[dic[@"key"]]  = textStr;
        };
        [cell setClickCarNumBlock:^{
            [self.view endEditing:YES];
            [UIView animateWithDuration:0.3 animations:^{
                self.carNumView.Y = 0;
            }];
        }];
        return cell;
    }
    // 备注
    if([dic[@"key"]  isEqualToString:@"remark"]){
       LXRemarkTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
       [cell setClipsToBounds:YES];
       cell.textView.placeholder = @"请输入 ~";
       cell.textView.maxLength = 200;
       cell.remarkTitle.text = dic[@"title"];
       cell.textView.text = self.loanDic[dic[@"key"]];
       [cell setTextViewEdit:^(NSString * _Nonnull textStr) {
           self.loanDic[dic[@"key"]]  = textStr;
       }];
       return cell;
   }
    
    
    
    LXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setClipsToBounds:YES];
    

    cell.cellDic = dic;
    cell.textFiled.hidden = NO;
    cell.subTitleLab.hidden = YES;
    cell.textFiled.text = self.loanDic[dic[@"key"]];
    if ([dic[@"key"] isEqualToString:@"carBrand"] && !StringIsEmpty(self.loanDic[dic[@"key"]])) {
        cell.textFiled.hidden = YES;
        cell.subTitleLab.hidden = NO;
        cell.subTitleLab.text = self.loanDic[dic[@"key"]];
    }
    
   
    
    
    cell.textfiledChangeBlock = ^(NSString * _Nonnull textStr) {
        self.loanDic[dic[@"key"]]  = textStr;
    };
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic = self.data[indexPath.row];
    self.selDic = dic;
    NSString * selName = dic[@"sel"];
    SEL action = NSSelectorFromString(selName);
    if ([self respondsToSelector:action]) {
        [self performSelector:action withObject:nil afterDelay:0];
    }

}

#pragma mark -- 选择注册日期
-(void)clickRegisterTime{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"registerTime"] withIsMaxDate:YES withNoForward:NO WithTips:@"请选择注册日期"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
        [weakSelf.tableView reloadData];
    };
}

#pragma mark -- 选择年检日期
-(void)clickYearCheckUpTimeScope{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"yearCheckUpTimeScope"] withIsMaxDate:YES withNoForward:NO WithTips:@"请选择年检日期"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
        [weakSelf.tableView reloadData];
    };
}
#pragma mark -- 选择保险日期
-(void)clickInsureTime{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"insureTime"] withIsMaxDate:YES withNoForward:NO WithTips:@"请选保险日期"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
        [weakSelf.tableView reloadData];
    };
}
#pragma mark -- 选择车辆品牌
- (void)clickCarBrand{
    

    LXChooseBrandViewController *vc = [[LXChooseBrandViewController alloc] init];
    [vc setToSendValue:^(PinPaiChildrenModel * _Nonnull model) {
       
        [self.loanDic setValue:[NSString stringWithFormat:@"%@ %@",model.parent,model.value] forKey:@"carBrand"];
        self.carBrand = model.parent;
        self.carMode = model.value;
        [self.tableView reloadData];
    }];
    [self pushToNextController:vc];
}

- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc]init];
    }
    return _data;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, ScreenWidth, kScreenHeight - LL_StatusBarAndNavigationBarHeight - kComHeight - 10) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = White_Color;
        _tableView.contentInset = UIEdgeInsetsMake(0,0,kComHeight + 10,0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _tableView;
}
-(void)setBottomView{
    
    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(15, kScreenHeight - kComHeight - 10, kScreenWidth/2 - 30, kComHeight)];
    btn1.backgroundColor = UIColor.whiteColor;
    btn1.layer.cornerRadius = 3;
    btn1.layer.masksToBounds = YES;
    btn1.layer.borderWidth = 1;
    btn1.layer.borderColor = mainColor.CGColor;
    [btn1 setTitle:@"查看全部" forState:UIControlStateNormal];
    [btn1 setTitleColor:mainColor forState:UIControlStateNormal];
    btn1.titleLabel.font = Fond16;
    btn1.tag = 1;
    [self.view addSubview:btn1];
    [btn1 addTarget:self action:@selector(clickBottomView:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(MaxX(btn1)+15, kScreenHeight - kComHeight - 10, kScreenWidth/2 - 30, kComHeight)];
    btn2.backgroundColor = mainColor;
    btn2.layer.cornerRadius = 3;
    btn2.layer.masksToBounds = YES;
    btn2.tag = 2;
    [btn2 setTitle:@"确认新增" forState:UIControlStateNormal];
    [btn2 setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    btn2.titleLabel.font = Fond16;
    [self.view addSubview:btn2];
    [btn2 addTarget:self action:@selector(clickBottomView:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

-(void)clickBottomView:(UIButton *)sender{
    
    // 查看全部
    if (sender.tag==1) {
        
        [Tool showAlertTitle:@"温馨提示" msg:@"当前数据未保存, 是否前往查看全部空闲车辆?" cancelTitle:@"取消" commitBtnTitle:@"前往" cancelHandlerBlock:^{
        } commitHandlerBlock:^{
            
//            LXAllCarListViewController *vc = [[LXAllCarListViewController alloc] init];
//            vc.isShowAdd = YES;
//            [self pushToNextController:vc];
            
            
            AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            UIViewController *controller = appDelegate.window.rootViewController;
            LiXingMainTabbarVC *rvc = (LiXingMainTabbarVC *)controller;
            [rvc setSelectedIndex:3];//需要跳转的tabBar的索引，tabBar默认的索引是从0开始的。
            [self.navigationController popToRootViewControllerAnimated:YES];
            
            
        } onVC:self];
        
        return;
    }
    
    if (StringIsEmpty(self.frontUrl)) {
        showAlert(@"请上传行驶证正面");
        return;
    }
    
    if (StringIsEmpty(self.backUrl)) {
        showAlert(@"请上传行驶证副页");
        return;
    }
    
    if (StringIsEmpty(self.bevelUrl)) {
        showAlert(@"请上传车辆45°照片");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"carNum"])){
        showAlert(@"车牌号不能为空");
        return;
    }
    
    if (StringIsEmpty(self.carBrand)){
        showAlert(@"品牌不能为空");
        return;
    }
    
//    if (StringIsEmpty(self.loanDic[@"vinNo"])){
//        showAlert(@"车架号不能为空");
//        return;
//    }
    if (StringIsEmpty(self.loanDic[@"registerTime"])){
        showAlert(@"注册日期不能为空");
        return;
    }
//    if (StringIsEmpty(self.loanDic[@"engineNo"])){
//        showAlert(@"发动机号不能为空");
//        return;
//    }
    if (StringIsEmpty(self.loanDic[@"kilometers"])){
        showAlert(@"行驶里程不能为空");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"yearCheckUpTimeScope"])){
        showAlert(@"年检时间不能为空");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"insureTime"])){
        showAlert(@"保险时间不能为空");
        return;
    }
//    NSString *operPlatformStr;
//    //运行品台
//    for (NSDictionary *mm in self.operPlatformData) {
//        if ([mm[@"id"] intValue] == self.operPlatform) {
//            operPlatformStr = mm[@"title"];
//            break;
//        }
//    }

//    //车辆类型
//    NSString *leaseCarTypeStr;
//    for (NSDictionary *mm in self.leaseCarTypeData) {
//        if ([mm[@"id"] intValue] == self.leaseCarType) {
//            leaseCarTypeStr = mm[@"title"];
//            break;
//        }
//    }
//
//    // 运营性质 （1：营运，2：非营运）
//    NSString *carServiceTypeStr;
//    for (NSDictionary *mm in self.carServiceTypeData) {
//        if ([mm[@"id"] intValue] == self.carServiceType) {
//            carServiceTypeStr = mm[@"title"];
//            break;
//        }
//    }
    
//    // 车辆归属 （1：企业，2：个人）
//    NSString *ascriptionStr;
//    if (self.ascription == 1) {
//        ascriptionStr = @"企业";
//    }
//    else if(self.ascription == 2) {
//        ascriptionStr = @"个人";
//    }
    
    
    NSDictionary *dic = @{
        
        @"vehicleLicenseZUrl":self.frontUrl,// 正面照
        @"vehicleLicenseFUrl":self.backUrl,// 背面照
        @"car45Url":self.bevelUrl,// 45°照片
        @"carNum":[NSString stringWithFormat:@"%@%@",self.carNumStr,self.loanDic[@"carNum"]],// 车牌号
        @"carBrand":self.carBrand,// 品牌
        @"carMode":self.carMode,// 车型
        @"kilometers":self.loanDic[@"kilometers"],// 行驶里程
        @"vinNo":self.loanDic[@"vinNo"],// 车架号
        @"registerTime":self.registerTime,// 注册时间
        @"engineNo":self.loanDic[@"engineNo"],// 发动机号
        @"operPlatform":@(self.operPlatform),//运行平台
        @"leaseCarType":@(self.leaseCarType),//车辆类型
        @"energyType":@(self.energyType),//能源类型
        @"carServiceType":@(self.carServiceType),//营运性质
        @"ascription":@(self.ascription),//所有人
        @"yearCheckUpTimeScope":self.loanDic[@"yearCheckUpTimeScope"],//年检
        @"insureTime":self.loanDic[@"insureTime"],//保险
        @"remark":self.loanDic[@"remark"],
        
    };

    [MSNetworkManager postRevertUrl:TCarAdd_URL params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            if (isSuccess) {
                showAlert(@"车辆添加成功");
                kUserDefaultSetBool(YES, kRefreshStr);
                [self.navigationController popViewControllerAnimated:YES];
            }

        }
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        
    } withShowHud:YES];

    
}

#pragma mark --  车辆归属数据源
- (NSArray *)ascriptionData{
    
    _ascriptionData = @[
    
        @{
            @"title":@"企业",
            @"sel":@(NO),
            @"id":@"1"
        }.mutableCopy,
        @{
            @"title":@"个人",
            @"sel":@(NO),
            @"id":@"2"
        }.mutableCopy,
    ];
    for (NSMutableDictionary *dic in _ascriptionData) {
        if ([dic[@"id"] intValue] == self.ascription) {
            [dic setValue:@(YES) forKey:@"sel"];
            break;
        }
    }
    return _ascriptionData;
}
#pragma mark -- 运行性质数据源
- (NSArray *)carServiceTypeData{
    
    
    NSArray *carServiceTypeAry = [[NSUserDefaults standardUserDefaults] valueForKey:k_YXXZ];
    
    _carServiceTypeData = [NSMutableArray array];

    if (!ObjectIsEmpty(carServiceTypeAry)) {
        for (int i = 0; i<carServiceTypeAry.count; i++) {
            NSDictionary *mm = carServiceTypeAry[i];
            NSDictionary *dic = @{
                @"title":mm[@"label"],
                @"sel":@(NO),
                @"id":mm[@"value"],
            }.mutableCopy;
            [_carServiceTypeData addObject:dic];
        }
    }
    
    for (NSMutableDictionary *dic in _carServiceTypeData) {
        if ([dic[@"id"] intValue] == self.carServiceType) {
            [dic setValue:@(YES) forKey:@"sel"];
            break;
        }
    }
    return _carServiceTypeData;
}
#pragma mark --  能源类型数据源
- (NSArray *)energyTypeData{
    
//    _energyTypeData = @[
//
//        @{
//            @"title":@"电车",
//            @"sel":@(NO),
//            @"id":@"1"
//        }.mutableCopy,
//        @{
//            @"title":@"油车",
//            @"sel":@(NO),
//            @"id":@"2"
//        }.mutableCopy,
//        @{
//            @"title":@"油电混动",
//            @"sel":@(NO),
//            @"id":@"3"
//        }.mutableCopy,
//    ];
//    for (NSMutableDictionary *dic in _energyTypeData) {
//        if ([dic[@"id"] intValue] == self.energyType) {
//            [dic setValue:@(YES) forKey:@"sel"];
//            break;
//        }
//    }
//    return _energyTypeData;
    
    
    NSArray *energeTypeAry = [[NSUserDefaults standardUserDefaults] valueForKey:k_NYLX];
    
    _energyTypeData = [NSMutableArray array];

    if (!ObjectIsEmpty(energeTypeAry)) {
        
        for (int i = 0; i<energeTypeAry.count; i++) {
            
            NSDictionary *mm = energeTypeAry[i];
            NSDictionary *dic = @{
                @"title":mm[@"label"],
                @"sel":@(NO),
                @"id":mm[@"value"],
            }.mutableCopy;
            [_energyTypeData addObject:dic];
        }
    }

    for (NSMutableDictionary *dic in _energyTypeData) {
        if ([dic[@"id"] intValue]==self.energyType) {
            [dic setValue:@(YES) forKey:@"sel"];
            break;
        }
    }
    return _energyTypeData;
    
    
    
}
#pragma mark -- 车辆类型数据源
- (NSArray *)leaseCarTypeData{
    
    
    NSArray *carTypeAry = [[NSUserDefaults standardUserDefaults] valueForKey:k_CLLX];
    
    _leaseCarTypeData = [NSMutableArray array];

    if (!ObjectIsEmpty(carTypeAry)) {
        
        for (int i = 0; i<carTypeAry.count; i++) {
            
            NSDictionary *mm = carTypeAry[i];
            NSDictionary *dic = @{
                @"title":mm[@"label"],
                @"sel":@(NO),
                @"id":mm[@"value"],
            }.mutableCopy;
            [_leaseCarTypeData addObject:dic];
        }
    }

    for (NSMutableDictionary *dic in _leaseCarTypeData) {
        if ([dic[@"id"] intValue]==self.leaseCarType) {
            [dic setValue:@(YES) forKey:@"sel"];
            break;
        }
    }
    return _leaseCarTypeData;
}
#pragma mark -- 运行平台数据源
- (NSArray *)operPlatformData{
    
    
    NSArray *operPlatformAry = [[NSUserDefaults standardUserDefaults] valueForKey:k_YXPT];
    
    _operPlatformData = [NSMutableArray array];

    if (!ObjectIsEmpty(operPlatformAry)) {
        
        for (int i = 0; i<operPlatformAry.count; i++) {
            
            NSDictionary *mm = operPlatformAry[i];
            NSDictionary *dic = @{
                @"title":mm[@"label"],
                @"sel":@(NO),
                @"id":mm[@"value"],
            }.mutableCopy;
            [_operPlatformData addObject:dic];
        }
    }
    
 
    for (NSMutableDictionary *dic in _operPlatformData) {
        if ([dic[@"id"] intValue] == self.operPlatform) {
            [dic setValue:@(YES) forKey:@"sel"];
            break;
        }
    }
    return _operPlatformData;
}

- (UIView *)carNumView{
    if (!_carNumView) {
        
        _carNumView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight + 100, ScreenWidth, ScreenHeight)];
        _carNumView.backgroundColor = [UIColor colorWithRed:10.f/255 green:10.f/255 blue:10.f/255 alpha:0.6];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenView:)];
        tap.delegate = self;
        [_carNumView addGestureRecognizer:tap];

        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake(ScreenWidth/9 ,50);
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        collectionView.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = [UIColor colorWithHexString:@"#D7DBE0"];
        self.collectionView = collectionView;
        collectionView.frame = CGRectMake(0, ScreenHeight - 240, ScreenWidth, 240);
        [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([LXChooseCarNumCell  class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([LXChooseCarNumCell class])];
        [_carNumView addSubview:collectionView];
    }
    return _carNumView;
}
- (NSMutableArray *)carNumData{
    if (!_carNumData) {
        _carNumData = [NSMutableArray arrayWithObjects:@"京",@"沪",@"浙",@"苏",@"粤",@"鲁",@"晋",@"冀",@"豫",@"川",@"渝",@"辽",@"吉",@"黑",@"皖",@"鄂",@"湘",@"赣",@"闽",@"陕",@"甘",@"宁",@"蒙",@"津",@"贵",@"云",@"桂",@"琼",@"青",@"新",@"藏", nil];
    }
    return _carNumData;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.carNumData.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LXChooseCarNumCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LXChooseCarNumCell class]) forIndexPath:indexPath];
    NSString *str = self.carNumData[indexPath.row];
    cell.carNumLB.text = str;
    return cell;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.carNumView.Y = ScreenHeight + 100;
    } completion:^(BOOL finished) {
        
        self.carNumStr = self.carNumData[indexPath.row];
        [self.tableView reloadData];
    }];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:self.collectionView]) {
          return NO;
      }
      return YES;
}

-(void)hiddenView:(UITapGestureRecognizer *)tap{
    [UIView animateWithDuration:0.3 animations:^
     {
        self.carNumView.Y = ScreenHeight + 100;
        
    }];
}




@end
